home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / EMail.java < prev    next >
Text File  |  1998-10-28  |  13KB  |  349 lines

  1. /*
  2.     A basic extension of the java.applet.Applet class
  3.  */
  4. //package symantec.itools.awt;
  5. import java.awt.*;
  6. import java.applet.*;
  7. import java.net.*;
  8. import java.io.*;
  9.  
  10. import symantec.itools.awt.LabelHTMLLink;
  11. import symantec.itools.awt.WrappingLabel;
  12. public class EMail extends Applet {
  13.  
  14.  
  15.     public void init() {
  16.         super.init();
  17.  
  18.         //{{INIT_CONTROLS
  19.         setLayout(null);
  20.         setBackground(java.awt.Color.lightGray);
  21.         setSize(666,460);
  22.         title.setText("E-Mail Response Form");
  23.         title.setAlignment(java.awt.Label.CENTER);
  24.         title.setBounds(93,3,411,28);
  25.         title.setFont(new Font("Dialog", Font.BOLD, 24));
  26.         add(title);
  27.         try {
  28.             info.setText("The area below is for configuration, it is not visible to the users at run time. Click the text field and set each text property in the Property List according to the instructions to the left of the field.");
  29.         }
  30.         catch(java.beans.PropertyVetoException e) { }
  31.         try {
  32.             info.setAlignStyle(symantec.itools.awt.WrappingLabel.ALIGN_CENTERED);
  33.         }
  34.         catch(java.beans.PropertyVetoException e) { }
  35.         info.setVisible(false);
  36.         info.setBounds(33,179,552,35);
  37.         add(info);
  38.         try {
  39.             URLforwardLabel.setText("Set the forwarding URL of the page you want to link to after a successful mail send");
  40.         }
  41.         catch(java.beans.PropertyVetoException e) { }
  42.         URLforwardLabel.setVisible(false);
  43.         URLforwardLabel.setBounds(33,330,326,31);
  44.         add(URLforwardLabel);
  45.         serverResponse.setVisible(false);
  46.         serverResponse.setBounds(383,410,237,40);
  47.         add(serverResponse);
  48.         successArea.setText("Thanks for registering!");
  49.         successArea.setVisible(false);
  50.         successArea.setBounds(383,365,237,41);
  51.         add(successArea);
  52.         mailServer.setText("mail.yourmailserver.com");
  53.         mailServer.setVisible(false);
  54.         mailServer.setBounds(383,302,237,24);
  55.         mailServer.setBackground(java.awt.Color.white);
  56.         add(mailServer);
  57.         subjectLine.setText("Registration Email Response");
  58.         subjectLine.setVisible(false);
  59.         subjectLine.setBounds(383,272,237,24);
  60.         add(subjectLine);
  61.         recipientName.setText("youremail@yourmail.com");
  62.         recipientName.setVisible(false);
  63.         recipientName.setBounds(383,244,237,24);
  64.         add(recipientName);
  65.         senderName.setText("youremail@yourmail.com");
  66.         senderName.setVisible(false);
  67.         senderName.setBounds(383,216,237,24);
  68.         add(senderName);
  69.         URLforward.setText("http://www.yourmailweb.com");
  70.         URLforward.setVisible(false);
  71.         URLforward.setBounds(383,332,237,24);
  72.         add(URLforward);
  73.         try {
  74.             serverResponseLabel.setText("This window is for server response, change its Visible property to view the server execution commands for debugging.");
  75.         }
  76.         catch(java.beans.PropertyVetoException e) { }
  77.         serverResponseLabel.setVisible(false);
  78.         serverResponseLabel.setBounds(33,410,301,49);
  79.         add(serverResponseLabel);
  80.         senderLabel.setText("Set the email address for the email to be marked as from");
  81.         senderLabel.setVisible(false);
  82.         senderLabel.setBounds(33,215,352,23);
  83.         add(senderLabel);
  84.         recipientLabel.setText("Set the email address for the response form to send to");
  85.         recipientLabel.setVisible(false);
  86.         recipientLabel.setBounds(33,242,318,23);
  87.         add(recipientLabel);
  88.         subjectLabel.setText("Set the subject that the Email will have");
  89.         subjectLabel.setVisible(false);
  90.         subjectLabel.setBounds(33,270,317,23);
  91.         add(subjectLabel);
  92.         mailServerLabel.setText("Set the mail server you will be using");
  93.         mailServerLabel.setVisible(false);
  94.         mailServerLabel.setBounds(33,300,317,23);
  95.         add(mailServerLabel);
  96.         try {
  97.             successAreaLabel.setText("Type in any text you wish displayed to the user after sending the mail");
  98.         }
  99.         catch(java.beans.PropertyVetoException e) { }
  100.         successAreaLabel.setVisible(false);
  101.         successAreaLabel.setBounds(33,370,301,34);
  102.         add(successAreaLabel);
  103.         button1.setLabel("Back to the site");
  104.         add(button1);
  105.         button1.setBounds(504,7,122,22);
  106.         button1.setVisible(false);
  107.         panel1.setLayout(null);
  108.         panel1.setBounds(15,31,618,145);
  109.         panel1.add(firstName);
  110.         firstName.setBounds(73,5,104,21);
  111.         panel1.add(lastName);
  112.         lastName.setBounds(251,2,108,21);
  113.         computers.addItem("1");
  114.         computers.addItem("2");
  115.         try {
  116.             computers.select(0);
  117.         }
  118.         catch (IllegalArgumentException e) { }
  119.         computers.setBackground(java.awt.Color.white);
  120.         panel1.add(computers);
  121.         computers.setBounds(529,3,69,25);
  122.         country.addItem("USA");
  123.         country.addItem("Mexico");
  124.         try {
  125.             country.select(0);
  126.         }
  127.         catch (IllegalArgumentException e) { }
  128.         country.setBackground(java.awt.Color.white);
  129.         panel1.add(country);
  130.         country.setBounds(169,36,102,25);
  131.         heardAbout.addItem("Advert");
  132.         heardAbout.addItem("Commercial");
  133.         try {
  134.             heardAbout.select(0);
  135.         }
  136.         catch (IllegalArgumentException e) { }
  137.         heardAbout.setBackground(java.awt.Color.white);
  138.         panel1.add(heardAbout);
  139.         heardAbout.setBounds(427,33,111,25);
  140.         heardAboutLabel.setText("How you heard about us:");
  141.         panel1.add(heardAboutLabel);
  142.         heardAboutLabel.setBounds(280,34,142,23);
  143.         coutryLabel.setText("Country:");
  144.         panel1.add(coutryLabel);
  145.         coutryLabel.setBounds(101,35,50,23);
  146.         panel1.add(comments);
  147.         comments.setBounds(82,66,513,48);
  148.         send.setLabel("Send");
  149.         panel1.add(send);
  150.         send.setBounds(82,121,108,24);
  151.         newButton.setLabel("Reset");
  152.         panel1.add(newButton);
  153.         newButton.setBounds(487,121,108,24);
  154.         lastNameLabel.setText("Last Name:");
  155.         panel1.add(lastNameLabel);
  156.         lastNameLabel.setBounds(181,3,65,23);
  157.         commentsLabel.setText("Comments:");
  158.         panel1.add(commentsLabel);
  159.         commentsLabel.setBounds(10,84,67,23);
  160.         messageLabel.setText("First Name:");
  161.         panel1.add(messageLabel);
  162.         messageLabel.setBounds(3,4,66,23);
  163.         computersLabel.setText("How many PC\'s in your office:");
  164.         panel1.add(computersLabel);
  165.         computersLabel.setBounds(363,2,162,23);
  166.         add(panel1);
  167.         //}}
  168.     
  169.         //{{REGISTER_LISTENERS
  170.         SymAction lSymAction = new SymAction();
  171.         send.addActionListener(lSymAction);
  172.         newButton.addActionListener(lSymAction);
  173.         button1.addActionListener(lSymAction);
  174.         //}}
  175.     }
  176.     
  177.     //Resets all TextFields and textArea in preparation for a new message.
  178.     void prepForNewMessage(java.awt.event.ActionEvent event)
  179.     {
  180.         comments.setText("");
  181.         serverResponse.setText("");
  182.         firstName.setText("");
  183.         lastName.setText("");
  184.  
  185.     }
  186.  
  187.     //This is the method responsible for establishing a communications
  188.     //path to the mail server and for sending SMTP commands.
  189.     //  Step 1:  Create a new socket object using the name of the mail
  190.     //             server and the port number.
  191.     //  Step 2:  Create a printstream object based on the new socket
  192.     //  Step 3:  Send the mail server the appropriate SMTP commands
  193.     //             using the printstream.
  194.     //  Step 4:  This sample also captures and displays status messages
  195.     //             return from the mail server.  See getReply() method
  196.     //             below.
  197.     //  Step 5:  Release the connection to the server.
  198.     //  Step 6:  Close the printstream and the socket.
  199.     void sendMessage(java.awt.event.ActionEvent event)
  200.     {
  201.         //Insert sending host name below...
  202.         String sHostName = new String(mailServer.getText());
  203.  
  204.         //Insert sendmail port number on sending host machin below...
  205.         //(Note:  Port will typically be port #25.)
  206.         int portNum = 25;
  207.  
  208.         try
  209.         {
  210.             //Step 1
  211.             Socket outgoing = new Socket(sHostName, portNum);
  212.  
  213.             //Step 2
  214.             PrintWriter pw = new PrintWriter(outgoing.getOutputStream());
  215.  
  216.             //Steps 3 & 4
  217.             pw.println("HELO " + sHostName);
  218.             serverResponse.append(getReply(outgoing) + "\n");
  219.  
  220.             pw.println("MAIL FROM: " + senderName.getText());
  221.             serverResponse.append(getReply(outgoing) + "\n");
  222.  
  223.             pw.println("RCPT TO: " + recipientName.getText());
  224.             serverResponse.append(getReply(outgoing) + "\n");
  225.  
  226.             pw.println("DATA");
  227.             serverResponse.append(getReply(outgoing) + "\n");
  228.  
  229.             pw.println("Subject: "  + subjectLine.getText() + "\n" 
  230.             + firstName.getText() + "\n"
  231.             + lastName.getText() + "\n"
  232.             + country.getSelectedItem() + "\n" 
  233.             + computers.getSelectedItem() + "\n" 
  234.             + heardAbout.getSelectedItem() + "\n" 
  235.             + comments.getText() + "\n" 
  236.             + "\n" + "." + "\n");
  237.  
  238.             serverResponse.append(getReply(outgoing) + "\n");
  239.  
  240.             //Step 5
  241.             pw.println("QUIT");
  242.             serverResponse.append(getReply(outgoing) + "\n");
  243.  
  244.             //Step 6
  245.             pw.close();
  246.             outgoing.close();
  247.         }
  248.         catch (IOException Ex)
  249.         {
  250.             System.out.println(Ex.getMessage());
  251.         }
  252.         //hides the form once sent
  253.         //{{CONNECTION
  254.         // Hide the KeyPressManagerPanel
  255.         //Customize message after send of mail
  256.         //shows the HTML link back to where you want them to go
  257.         panel1.setVisible(false);
  258.         title.setText(successArea.getText());
  259.         button1.setVisible(true);
  260.         //}}
  261.  
  262.  
  263.  
  264.     }
  265.  
  266.     //This method will capture status message returned by the mail server
  267.     //and display in the the Server Response TextArea on the Applet.
  268.     //Step 1:  Create an InputStream for the socket object created above that gets passed
  269.     //          into this method.
  270.     //Step 2:  Read a line of the InputStream and return the line as a string for display.
  271.     String getReply(Socket incoming)
  272.     {
  273.         try
  274.         {
  275.             //Step 1
  276.             BufferedReader myReader = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
  277.  
  278.             //Step 2            
  279.             return myReader.readLine();
  280.         }
  281.         catch (IOException Ex)
  282.         {
  283.             return Ex.getMessage();
  284.         }
  285.     }
  286.  
  287.     //{{DECLARE_CONTROLS
  288.     java.awt.Label title = new java.awt.Label();
  289.     symantec.itools.awt.WrappingLabel info = new symantec.itools.awt.WrappingLabel();
  290.     symantec.itools.awt.WrappingLabel URLforwardLabel = new symantec.itools.awt.WrappingLabel();
  291.     java.awt.TextArea serverResponse = new java.awt.TextArea();
  292.     java.awt.TextArea successArea = new java.awt.TextArea();
  293.     java.awt.TextField mailServer = new java.awt.TextField();
  294.     java.awt.TextField subjectLine = new java.awt.TextField();
  295.     java.awt.TextField recipientName = new java.awt.TextField();
  296.     java.awt.TextField senderName = new java.awt.TextField();
  297.     java.awt.TextField URLforward = new java.awt.TextField();
  298.     symantec.itools.awt.WrappingLabel serverResponseLabel = new symantec.itools.awt.WrappingLabel();
  299.     java.awt.Label senderLabel = new java.awt.Label();
  300.     java.awt.Label recipientLabel = new java.awt.Label();
  301.     java.awt.Label subjectLabel = new java.awt.Label();
  302.     java.awt.Label mailServerLabel = new java.awt.Label();
  303.     symantec.itools.awt.WrappingLabel successAreaLabel = new symantec.itools.awt.WrappingLabel();
  304.     java.awt.Button button1 = new java.awt.Button();
  305.     java.awt.Panel panel1 = new java.awt.Panel();
  306.     java.awt.TextField firstName = new java.awt.TextField();
  307.     java.awt.TextField lastName = new java.awt.TextField();
  308.     java.awt.Choice computers = new java.awt.Choice();
  309.     java.awt.Choice country = new java.awt.Choice();
  310.     java.awt.Choice heardAbout = new java.awt.Choice();
  311.     java.awt.Label heardAboutLabel = new java.awt.Label();
  312.     java.awt.Label coutryLabel = new java.awt.Label();
  313.     java.awt.TextArea comments = new java.awt.TextArea();
  314.     java.awt.Button send = new java.awt.Button();
  315.     java.awt.Button newButton = new java.awt.Button();
  316.     java.awt.Label lastNameLabel = new java.awt.Label();
  317.     java.awt.Label commentsLabel = new java.awt.Label();
  318.     java.awt.Label messageLabel = new java.awt.Label();
  319.     java.awt.Label computersLabel = new java.awt.Label();
  320.     //}}
  321.  
  322.  
  323.     class SymAction implements java.awt.event.ActionListener
  324.     {
  325.         public void actionPerformed(java.awt.event.ActionEvent event)
  326.         {
  327.             Object object = event.getSource();
  328.             if (object == button1)
  329.                 button1_ActionPerformed(event);
  330.             else if (object == send)
  331.                 sendMessage(event);
  332.             else if (object == newButton)
  333.                 prepForNewMessage(event);
  334.         }
  335.     }
  336.  
  337.     void button1_ActionPerformed(java.awt.event.ActionEvent event)
  338.     {
  339.         try
  340.         {
  341.             getAppletContext().showDocument(new java.net.URL(URLforward.getText()));
  342.         } 
  343.         catch (java.net.MalformedURLException error) 
  344.         {
  345.             error.printStackTrace();
  346.         }
  347.     }
  348. }
  349.